<!-- 新闻中心 -->
<template>
  <div class="journalism">
    <header v-if="Back.news_back"
      :style="'background: url(' + Back.news_back + ') no-repeat 50% 50%;'">
      <span>{{ i18n.journalism.journalism }}</span>
    </header>
    <nav>
      <main>
        <el-date-picker
          class="picker"
          @change="change_time"
          v-model="date"
          type="month"
          placeholder="选择年月"
        >
        </el-date-picker>
        <div class="search">
          <p class="input_back">
            <img src="@/assets/navigation/4.png" />
            <input
              @keyup.enter="handleEnter"
              v-model="from.keyword"
              type="text"
              :placeholder="i18n.navigation.search"
            />
          </p>
        </div>
      </main>
    </nav>
    <div
      class="list"
      v-for="val in list"
      :key="val.id"
      @click="$method.skip('/journalism_detail?id='+val.id)"
    >
      <main>
        <h3>{{ val.title }}</h3>
        <span>{{ val.abstract }}</span>
        <p>
          <span>
            <i>
              <img :src="require('@/assets/journalism/2.png')" />
              {{val.view_num}}
            </i>
            <!-- <i>
              <img :src="require('@/assets/journalism/3.png')" />
              {{ i18n.journalism.share }}
            </i> -->
          </span>
          <span v-if="width > 768"
            >{{ i18n.journalism.time }}：{{ val.create_time }}</span
          >
        </p>
      </main>
      <span><img :src="val.image" /></span>
    </div>
    <el-pagination
      class="pagination"
      :small="width <= 768"
      background
      layout="prev, pager, next"
      :total="count"
      :page-size="from.page_size"
      @current-change="change"
    >
    </el-pagination>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import bus from "@/config/emit";

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      date: "",
      count: 0,
      list: [],
      Back: {},
      from: {
        cid: 4,
        page_no: 1,
        page_size: 10,
        t: "",
        keyword: "",
      },
    };
  },
  props: ["width"],
  //计算属性 类似于data概念
  computed: {
    i18n() {
      return this.$t("message");
    },
  },
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    change(e) {
      this.from.page_no = e;
      this.getArticle();
    },
    getArticle() {
      this.$getRequest("/index/getArticle", this.from).then((res) => {
        res.data.lists.forEach(val=>{
          let arr = val.create_time.split('')
          let index = arr.findIndex(val=>val===' ')
          arr.splice(index)
          val.create_time=arr.join('')
        })
        this.list = res.data.lists;
        this.count = res.data.count;
      });
    },
    handleEnter() {
        this.getArticle();
    },
    change_time(val) {
      this.from.t =
        val.getFullYear() + "-" + String(val.getMonth() + 1).padStart(2, "0");
        this.getArticle();
    },
    getBack() {
      this.$getRequest("/index/getBack").then((res) => {
        this.Back = res.data;
      });
    },
  },
  beforeCreate() {}, //生命周期 - 创建之前
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getBack();
    this.date = new Date();
    this.from.t =
      this.date.getFullYear() +
      "-" +
      String(this.date.getMonth() + 1).padStart(2, "0");
    bus.$on("lang", () => {
      if (this.$route.name == "/journalism") {
        this.getArticle();
      }
    });
    bus.$on("search", (keyword) => {
      if (this.$route.name == "/journalism") {
        this.from.keyword=keyword
        this.getArticle();
      }
    });
  },
  beforeMount() {}, //生命周期 - 挂载之前
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {
    this.from.keyword = this.$route.params.keyword || this.from.keyword;
    this.getArticle();
  }, //如果页面有keep-alive缓存功能，激活
  deactivated() {}, //如果页面有keep-alive缓存功能，非激活
};
</script>
<style lang="less">
.journalism {
  min-height: 80vh;
  & > .list:hover {
    & > span {
      & > img {
        transform: translate(-50%, -50%) scale(1.1);
      }
    }
  }
  & > .list {
    width: 17.5rem;
    height: 6.25rem;
    background-color: #fff;
    margin: 0 auto 0.5rem;
    display: flex;

    & > main {
      width: 9rem;
      height: 6.25rem;
      padding: 0.5625rem 0.75rem;
      box-sizing: border-box;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      & > p {
        color: #999999;
        font-size: 0.25rem;
        display: flex;
        align-items: center;
        justify-content: space-between;
        & > span {
          display: flex;
        }

        i {
          margin-right: 0.875rem;
          display: flex;
          align-items: center;

          & > img {
            width: 0.275rem;
            margin-right: 0.25rem;
          }
        }
      }

      & > span {
        font-size: 0.25rem;
        overflow: hidden;
        word-break: break-all;
        text-overflow: ellipsis;
        display: -webkit-box !important;
        -webkit-line-clamp: 8;
        -webkit-box-orient: vertical;
      }

      & > h3 {
        font-size: 0.3125rem;
        overflow: hidden;
        word-break: break-all;
        text-overflow: ellipsis;
        display: -webkit-box !important;
        -webkit-line-clamp: 1;
        -webkit-box-orient: vertical;
      }
    }

    & > span {
      width: 8.5rem;
      height: 6.25rem;
      position: relative;
      overflow: hidden;

      & > img {
        min-width: 100%;
        min-height: 6.25rem;
        height: 6.25rem;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        transition: all 0.3s;
      }
    }
  }

  & > nav {
    width: 100%;
    height: 1.25rem;
    margin-bottom: 0.5rem;
    background-color: #fff;

    & > main {
      width: 17.5rem;
      height: 100%;
      margin: 0 auto;
      display: flex;
      align-items: center;
      justify-content: space-between;

      & > .search {
        display: flex;
        align-items: center;

        & > p {
          width: 3rem;
          height: 0.55rem;
          border-radius: 0.275rem;
          background-color: #33333310;
          display: flex;
          align-items: center;
          padding: 0 0.1625rem;
          box-sizing: border-box;

          & > input {
            width: 2.1rem;
            height: 100%;
            font-size: 0.225rem;
            background-color: #ffffff00;
          }

          & > img {
            width: 0.25rem;
            margin-right: 0.1375rem;
          }
        }
      }

      & > .picker {
        width: 2.5rem;
        height: 0.8rem;

        input {
          height: 0.8rem;
        }

        .el-input__inner {
          padding: 0 0.125rem;
          font-size: 0.3rem;
        }

        .el-input__suffix,
        .el-input__prefix {
          display: none;
        }

        * {
          max-height: 0.8rem;
          line-height: 0.8rem;
        }
      }
    }
  }

  & > .pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 1.25rem;
  }

  & > header {
    width: 100%;
    height: 7.5rem;
    background-size: 100% 100% !important;
    color: #fff;
    font-size: 0.75rem;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
  }
}

@media screen and (max-width: 768px) {
  .journalism {
    & > .list {
      width: 33.5rem;
      height: 10rem;
      margin: 0 auto 1rem;

      & > main {
        width: 18.5rem;
        height: 10rem;
        padding: 1rem 1.5rem;

        & > span {
          font-size: 1rem;
          -webkit-line-clamp: 2;
        }

        & > p {
          font-size: 1rem;

          i {
            margin-right: 1rem;

            & > img {
              width: 0.8rem;
            }
          }
        }

        & > h3 {
          font-size: 1.3rem;
        }
      }

      & > span {
        width: 15rem;
        height: 10rem;

        & > img {
          min-width: 100%;
          min-height: 10rem;
          height: 10rem;
        }
      }
    }

    & > nav {
      height: 5rem;
      margin-bottom: 1rem;

      & > main {
        width: 33.5rem;

        & > .search {
          font-size: 1.4rem;

          & > p {
            width: 14rem;
            height: 2.5rem;
            border-radius: 1.25rem;
            padding: 0 1rem;

            & > input {
              width: 9rem;
              height: 100%;
              font-size: 1.2rem;
            }

            & > img {
              width: 1.4rem;
              margin-right: 1rem;
            }
          }
        }

        & > .picker {
          width: 6rem;
          height: 3rem;

          input {
            height: 3rem;
          }

          .el-input__inner {
            padding: 0 1rem;
            font-size: 1rem;
          }

          * {
            max-height: 3rem;
            line-height: 3rem;
          }
        }
      }
    }

    & > header {
      height: 20rem;
      background-size: auto 100% !important;
      font-size: 1.5rem;
    }
  }
}
</style>